package com.example.demo.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.bean.CmsMember;
import com.example.demo.common.Respones;
import com.example.demo.service.CmsMemberService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(tags = "会员信息管理服务")
@RestController
@RequestMapping("/member")
public class CmsMemberController {
	@Autowired
	private CmsMemberService memberservice;
	
	//按会员编号删除会员信息
	@ApiOperation(value = "按照会员编号，删除会员信息！")
	@DeleteMapping("/deleteById")
	public Respones<String> deleteById(String id){
		int count = memberservice.deleteById(id);
		return Respones.ok("成功删除" + count +"个成员！");	
	}
	
	//会员状态
	@ApiOperation(value = "审核会员")
	@ApiImplicitParams({
		@ApiImplicitParam(name="memId",value = "会员编号"),
		@ApiImplicitParam(name = "state",value = "状态",
							allowableValues = "0,1")
	})
	@PutMapping(value = "/updateState")
	public Respones<String> updateState(String memId,
			int state){
		boolean m = memberservice.updateState(memId, state);
		if(m) return Respones.ok("操作成功！");
		else return Respones.error("要更新的会员不存在！");
	}
	
	
	
	
	//保存或者更新会员信息
	@ApiOperation(value = "保存或者更新会员信息",
			notes = "如果传递参数时没有指明会员编号，那么执行插入操作")
	@PostMapping("/saveOrUpdateMember")
	public Respones<String> saveOrUpdate(CmsMember member){
	boolean b= memberservice.saveOrUpdateCmsMember(member);
	if(b)	return Respones.ok("操作成功!!!");
	else return Respones.error("操作失败!");
		
	}
	
	
	

	//获取所有会员信息
	@ApiOperation(value = "获取所有会员信息",notes="从数据库检索所有会员信息")
	@GetMapping("/all")
	public Respones<List<CmsMember>> queryAll(){
		//调用业务方法
		List<CmsMember> cm=memberservice.getAllMember();
		return Respones.ok(cm);
	}
	

}
